package com.tiancikeji.zaoke.app.dao.zone.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.tiancikeji.zaoke.app.dao.zone.ZoneDao;
import com.tiancikeji.zaoke.app.model.zone.Zone;

public class ZoneDaoImpl extends JdbcDaoSupport implements ZoneDao{

	private static final String TABLE = "`zaoke`.`zone`";
	private static final String QUERY_VIEW = "`id`, `name`, `type`, `parent_id`, `state`, `sequence`, `pick_start_time`, `pick_end_time`, `add_userid`, `add_time`, `lastmod_userid`,`lastmod_time`";

	public Set<Zone> getZonesByState(Integer[] states){
		String sql = "select " + QUERY_VIEW + " from " + TABLE + " where `state` in (";
		sql += StringUtils.join(states, ",");
		sql += ")";
		
		Set<Zone> result = new HashSet<Zone>();
		List<Zone> zones = getJdbcTemplate().query(sql, new RowMapper<Zone>() {

			public Zone mapRow(ResultSet rs, int rowNum) throws SQLException {
				Zone zone = new Zone();
				zone.setId(rs.getLong("id"));
				zone.setName(rs.getString("name"));
				zone.setType(rs.getInt("type"));
				zone.setParentId(rs.getLong("parent_id"));
				zone.setState(rs.getInt("state"));
				zone.setSequence(rs.getInt("sequence"));
				zone.setPickStartTime(rs.getString("pick_start_time"));
				zone.setPickEndTime(rs.getString("pick_end_time"));
				zone.setAddUserId(rs.getLong("add_userid"));
				zone.setAddTime(rs.getDate("add_time"));
				zone.setLastmodUserId(rs.getLong("lastmod_userid"));
				zone.setLastmodTime(rs.getDate("lastmod_time"));
				return zone;
			}
		});
		
		if(zones != null && zones.size() > 0)
			result.addAll(zones);
		
		return result;
	}
}
